/**
 * Copyright @ GTOMATO
 */
package com.linkwings.dbTemp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

/**
 * @author sam
 *
 */
public class DateUtils {
	protected static SimpleDateFormat sdf=new SimpleDateFormat("MMMMM yyyy", Locale.US);



	/**
	 * @author LK
	 *
	 */
	   public  static  java.util.Date  addDate(java.util.Date  date,  Integer  value,String type)  {
		   if(value==null){
			   return null;
		   }
           java.util.Calendar  c  =  java.util.Calendar.getInstance();
           if(type.equals("d")){
           c.setTimeInMillis(getMillis(date)  +  ((long)  value)  *  24  *  60*60  *  1000);
           }
           if(type.equals("h")){
               c.setTimeInMillis(getMillis(date)  +  ((long)  value) *  60*60  *  1000);
            }
           if(type.equals("m")){
               c.setTimeInMillis(getMillis(date)  +  ((long)  value) *  60  *  1000);
            }
           if(type.equals("s")){
               c.setTimeInMillis(getMillis(date)  +  ((long)  value) *  1000);
            }
           if(type.equals("ms")){
               c.setTimeInMillis(getMillis(date)  +  ((long)  value));
            }
           return  c.getTime();
	   }


	/**
	 * get the month below current month ,total 12 month
	 * @return
	 * @Author sam
	 * @date 2006-7-7
	 */
	public static List<String> getRecent12Month()
	{
		List<String> monthList=new ArrayList();
		Date now=new Date();
		String dateString=sdf.format(now);
		monthList.add(dateString);
		Date belowDate=null;
		for(int i=1;i<12;i++)
		{
			Calendar cal=Calendar.getInstance();
			cal.add(Calendar.MONTH ,-i);
			belowDate=cal.getTime();
			monthList.add(sdf.format(belowDate));
		}
		return monthList;
	}

	public static List<String> getRecent12Month(Locale alocale)
	{
		List<String> monthList=new ArrayList();
		Calendar cal_now=Calendar.getInstance(alocale);
		SimpleDateFormat csdf=new SimpleDateFormat("MMMMM yyyy", alocale);
		String dateString=csdf.format(cal_now.getTime());
		monthList.add(dateString);
		for(int i=1;i<12;i++)
		{
			Calendar cal=Calendar.getInstance(alocale);
			cal.add(Calendar.MONTH ,-i);
			monthList.add(csdf.format(cal.getTime()));
		}
		return monthList;
	}
	/**
	 *
	 * @param formatStr
	 * @return
	 * @throws ParseException
	 * @Author sam
	 * @date 2006-7-7
	 */
	public static Date getMonthDate(String formatStr) throws ParseException
	{
		return sdf.parse(formatStr);
	}

	/**
	 * with locale
	 * Date
	 * @param formatStr
	 * @param locale
	 * @return
	 * @throws ParseException
	 * @author sam
	 * 2006-9-1
	 */
	public static Date getMonthDate(String formatStr,Locale locale) throws ParseException
	{
		SimpleDateFormat csdf=new SimpleDateFormat("MMMMM yyyy", locale);
		return csdf.parse(formatStr);
	}
	/**
	 *
	 * @param formatStr
	 * @param rollMonth
	 * @return
	 * @throws ParseException
	 * @Author sam
	 * @date 2006-7-7
	 */
	public static Date getMonthDateWithRoll(String formatStr,int rollMonth) throws ParseException
	{

		Date srcDate=sdf.parse(formatStr);
		Calendar cal=Calendar.getInstance();
		cal.setTime(srcDate);
		cal.add(Calendar.MONTH,rollMonth);

		return cal.getTime();
	}
	/**
	 * with locale
	 * Date
	 * @param formatStr
	 * @param rollMonth
	 * @return
	 * @throws ParseException
	 * @author sam
	 * 2006-9-1
	 */
	public static Date getMonthDateWithRoll(String formatStr,int rollMonth,Locale locale) throws ParseException
	{
		SimpleDateFormat csdf=new SimpleDateFormat("MMMMM yyyy", locale);
		Date srcDate=csdf.parse(formatStr);
		Calendar cal=Calendar.getInstance(locale);
		cal.setTime(srcDate);
		cal.add(Calendar.MONTH,rollMonth);

		return cal.getTime();
	}




	public static String getCurrentYear()
	{
		Calendar cal=Calendar.getInstance();
		return String.valueOf(cal.get(Calendar.YEAR));
	}

	public static String getCurrentDay()
	{
		Calendar cal=Calendar.getInstance();
		return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
	}

	public static int getCurrentDayInt()
	{
		Calendar cal=Calendar.getInstance();
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	public static String getCurrentMonth()
	{
		Calendar cal=Calendar.getInstance();
		return String.valueOf(cal.get(Calendar.MONTH)+1);
	}

	/**
	 *
	 * terry
	 * Integer[]
	 * @param formatDay
	 * @return
	 * Nov 27, 2006
	 */
	protected static Integer[] splitDate(Date formatDay){
	    SimpleDateFormat sdf=new SimpleDateFormat("yyyy,M,d,H,m", Locale.US);
	    String sToday = sdf.format(formatDay);
	    String[] temp= StringUtils.split(sToday, ",");
	    Integer[] aToday = new Integer[temp.length];
	    Integer[] sourceday = {1970,1,1,0,0};
	    for (int i = 0; i < aToday.length; i++) {
	    	aToday[i]=Integer.parseInt(temp[i])-sourceday[i];
	    	//System.out.println(aToday[i]);
		}
    return aToday;
   }

	protected static Integer[] splitDateTime(Date formatDay){
	    SimpleDateFormat sdf=new SimpleDateFormat("yyyy,M,d,H,m,s", Locale.US);
	    String sToday = sdf.format(formatDay);
	    String[] temp= StringUtils.split(sToday, ",");
	    Integer[] aToday = new Integer[temp.length];
	    Integer[] sourceday = {1970,1,1,0,0,0};
	    for (int i = 0; i < aToday.length; i++) {
	    	aToday[i]=Integer.parseInt(temp[i])-sourceday[i];
	    	//System.out.println(aToday[i]);
		}
    return aToday;
   }

	public static long getMillis(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.getTimeInMillis();
	}

	public static Integer[] getCompareDate(Date date){
		if (date!=null) {
			long temp= Math.abs(getMillis(new Date()) - getMillis(date)) -(8 * 3600 * 1000);
			java.util.Calendar c = java.util.Calendar.getInstance();
	        c.setTimeInMillis(temp);
			return splitDate(c.getTime());
		}
		return null ;
	}

	public static Integer[] getCompareDateTime(Date date){
		if (date!=null) {
			long temp= Math.abs(getMillis(new Date()) - getMillis(date)) -(8 * 3600 * 1000);
			java.util.Calendar c = java.util.Calendar.getInstance();
	        c.setTimeInMillis(temp);
			return splitDateTime(c.getTime());
		}
		return null ;
	}



	/**
	 * thread to sleep 5 millsecond
	 * @return
	 */
	public static String getCurremtTimeMillisId()
	{
		try {
			Thread.sleep(5);
		} catch (InterruptedException e) {
			//do nothing
		}
		String returnTimeId=String.valueOf(System.currentTimeMillis());
		return returnTimeId;
	}

    public  static  int  diffDate(java.util.Date  date,  java.util.Date  date1)  {

    	int result=(int)(((getMillis(date)  -  getMillis(date1) )  /  (24  *  3600  *  1000)));
    	return result+1;
    }


   /**
    *
    * terry
    * java.util.Date
    * @param date
    * @param day
    * @return
    * Apr 27, 2007
    */
   public  static  java.util.Date  addDate(java.util.Date  date,  int  day)  {
               java.util.Calendar  c  =  java.util.Calendar.getInstance();
               c.setTimeInMillis(getMillis(date)  +  ((long)  day)  *  24  *  3600  *  1000);
               return  c.getTime();
   }


   public static boolean isValidDateStr(String date, String format) {
     try {
       SimpleDateFormat sdf = new SimpleDateFormat(format);
       sdf.setLenient(false);
       sdf.parse(date);
     }
     catch (ParseException e) {
       return false;
     }
     catch (IllegalArgumentException e) {
       return false;
     }
     return true;
     }

	// for test
	public static void main(String[] args)
	{

		Integer[] d =getCompareDateTime(new Date());
		for (Integer integer : d) {
			System.out.println(integer);

		}
		/* SimpleDateFormat sdf=new SimpleDateFormat("yyyy,M,d,H,m,s", Locale.US);
		 try {
			 Date date = sdf.parse("2007,1,18,17,20,20");
			 System.out.println(diffDate(date ,new Date())) ;
		} catch (ParseException e) {
			e.printStackTrace();
		}*/

		/*
		List<String> testList=getRecent12Month(Locale.CHINA);

		for(int i=0;i<testList.size();i++)
		{
			System.out.println(testList.get(i));
			try {
				Date tt=getMonthDate(testList.get(i), Locale.CHINA);
				System.out.println(tt);
				System.out.println(getMonthDateWithRoll(testList.get(i),1,Locale.CHINA));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
//			try {
//				Date tt=getMonthDateWithRoll(testList.get(i),1);
//				System.out.println("===>"+sdf.format(tt));
//			} catch (ParseException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}

	//	}
	}
}
